<?php
	//$check=$_POST['value_check'];
	echo "<p>KẾT QUẢ TÌM KIẾM</p>";
	$tukhoa=$_REQUEST['tukhoa'];
	$doituong=$_REQUEST['doituong'];
	if($doituong=="khachhang")
	{
		$kq=$knoi->queryCsdl("select makh,tenkh,mail,dienthoai from khachhang where makh not in (select makh from taikhoan) and (makh like '%$tukhoa%' or tenkh like '%$tukhoa%' or cmnd like '%$tukhoa%' or diachi like '%$tukhoa%' or dienthoai like '%$tukhoa%' or mail like '%$tukhoa%')");
		if(mysql_num_rows($kq)<1)
		{
			echo "<br><label class='stylelitle'>Không tìm thấy khách hàng với từ khóa \"".$tukhoa."\"!</label><br>";
		}
		else
		{
		$tongdong=mysql_num_rows($kq);
		$dong_trang=2;
		$sotrang=ceil($tongdong/$dong_trang);
		
		$limit="0,".$dong_trang;
		if(isset($_GET['page']))
		{
			$t=$_GET['page'];
			$ar=explode(":",$t);
			$limit=$ar[1];
		}
		if($sotrang>1)
			$kq=$knoi->queryCsdl("select makh,tenkh,mail,dienthoai from khachhang where makh not in (select makh from taikhoan) and (makh like '%$tukhoa%' or tenkh like '%$tukhoa%' or cmnd like '%$tukhoa%' or diachi like '%$tukhoa%' or dienthoai like '%$tukhoa%' or mail like '%$tukhoa%') limit $limit");
				?>
	<p class="styletitle" align="center">DANH SÁCH KHÁCH HÀNG</p>
		<table align="center" style="border:solid #FFFFFF; background-color:#F5F5F5">
			<tr class="stylelitle" style="text-align:center; background-color:#CCFFFF"><td width="6%">Stt</td>
			<td>MAKH</td>
			<td>Họ tên</td>
			<td>Điện thoại</td>
			<td>Email</td>
			<td>Chi tiết</td>
			<td>Xóa</td>
			</tr>
			<?php
				$i=1;
				while($n=mysql_fetch_array($kq))
				{
					if($i%2==0)
						$color='#FFFFCC';
					else
						$color='#FFFFFF';
			?>	
					<tr style="background-color:<?php echo $color; ?>" class="classtr"><td align="right"><?php echo $i; ?>&nbsp;</td><td>&nbsp;<?php echo $n['makh']; ?></td><td>&nbsp;<?php echo $n['tenkh']; ?></td><td>&nbsp;<?php echo $n['dienthoai']; ?></td><td>&nbsp;<?php echo $n['mail']; ?></td><td align="center"><a href="home_admin.php?module=thongtinkhachhang&makh=<?php echo $n['makh']; ?>"><img src="images/chitiet.png" border="0" title="Thông tin chi tiết" /></a></td><td align="center"><a href="home_admin.php?module=xoa&field=makh&table=khachhang&value=<?php echo $n['makh']; ?>"><img src="images/Sign-Error-icon1.png" border="0" title="Xóa khách hàng" /></a></td></tr>
			<?php
				$i++;	
				}
			?>
		</table>
<?php
		if($sotrang>1)
			{
		 	if($ar[0]!=1 && isset($_GET['page']))
				$color='#0033CC';
			else
				$color='#000000';
			$start="1:0,".$dong_trang;
			?>
			<div> Pages &nbsp;
			<?php
				echo "<a href='home_admin.php?module=timkiem&doituong=$doituong&tukhoa=$tukhoa&page=$start' style='color:$color'>1</a>";
			for($i=2;$i<=$sotrang;$i++)
			{
				if($ar[0]==$i)
					$color='#000000';
				else
					$color='#0033CC';
				$p=($i-1)*$dong_trang;
				$page=$i.":".$p.",".$dong_trang;
				echo "&nbsp;<a href='home_admin.php?module=timkiem&doituong=$doituong&tukhoa=$tukhoa&page=$page&i=$p' style='color:$color'>$i</a>";	
			}
		}
		}
		if($c<1)
			echo "<br>";
	?>
		<br /><label style="text-align:center" class="button"><a href="home_admin.php?module=themmoikhachhang" class="menulink"><img src="images/add.png" border="0">&nbsp;Khách hàng mới</a></label>
	<?php
	}
	else if($doituong=="tinraovat")
	{
		$kq=$knoi->queryCsdl("select matin,tieude,tentinhtp,tenht,ngaydang,ngayhethan,tenkh,tenloai from tinraovat a,khachhang b,tinhthanhpho c,hinhthuc d,loainhadat e where a.makh=b.makh and a.mattp=c.mattp and a.maht=d.maht and a.maloai=e.maloai and (tieude like '%$tukhoa%' or noidung like '%$tukhoa%' or a.diachi like '%$tukhoa%' or tenht like '%$tukhoa%' or tentinhtp like '%$tukhoa%' or tenkh like '%$tukhoa%' or tenloai like '%$tukhoa%' or tendn like '%$tukhoa%') order by tentinhtp,tenht,ngaydang desc");
		//echo "<br>nmhanh297</br>";
		$c=mysql_num_rows($kq);
		if(mysql_num_rows($kq)<1)
			echo "<br><label class='stylelitle'>Không tìm thấy tin rao vặt nào có từ khóa \"".$tukhoa."\"!</label>";
		else
		{
		$tongdong=mysql_num_rows($kq);
		$dong_trang=2;
		$sotrang=ceil($tongdong/$dong_trang);
		//echo "sotrang: ".$sotrang;
		$limit="0,".$dong_trang;
		if(isset($_GET['page']))
		{
			$t=$_GET['page'];
			//echo "<br>page: ".$t;
			$ar=explode(":",$t);
			$limit=$ar[1];
		}
		if($sotrang>1)
			$kq=$knoi->queryCsdl("select matin,tieude,tentinhtp,tenht,ngaydang,ngayhethan,tenkh,tenloai from tinraovat a,khachhang b,tinhthanhpho c,hinhthuc d,loainhadat e where a.makh=b.makh and a.mattp=c.mattp and a.maht=d.maht and a.maloai=e.maloai and (tieude like '%$tukhoa%' or noidung like '%$tukhoa%' or a.diachi like '%$tukhoa%' or tenht like '%$tukhoa%' or tentinhtp like '%$tukhoa%' or tenkh like '%$tukhoa%' or tenloai like '%$tukhoa%' or tendn like '%$tukhoa%') order by tentinhtp,tenht,ngaydang desc limit $limit");
				?>
		<table align="center" style="border:solid #FFFFFF; background-color:#F5F5F5">
			<tr class="stylelitle" style="text-align:center; background-color:#CCFFFF"><td>Stt</td><td>Tỉnh/Tp</td><td>Loại nhà đất</td><td>Tiêu đề</td><td>Ngày đăng</td><td>Ngày hết hạn</td><td>Khách hàng</td><td>Chi tiết</td><td>Xóa</td></tr>
		<?php
			$i=1;
			if(isset($_GET['i']))
				$i=$_GET['i'] + 1;
			while($n=mysql_fetch_array($kq))
			{
				if($i%2==0)
					$color='#FFFFCC';
				else
					$color='#FFFFFF';
		?>
			<tr class="classtr" style="text-align:left; background-color:<?php echo $color; ?>;"><td style="text-align:right"><?php echo $i; ?>&nbsp;</td>
			<td>&nbsp;<?php echo $n['tentinhtp']; ?></td><td>&nbsp;<?php echo $n['tenloai']; ?></td><td>&nbsp;<?php echo $n['tieude']; ?></td><td style="text-align:center"><?php echo $knoi->formatDate($n['ngaydang']); ?></td><td style="text-align:center"><?php echo $knoi->formatDate($n['ngayhethan']); ?></td><td>&nbsp;<?php echo $n['tenkh']; ?></td><td style="text-align:center"><a  href="home_admin.php?module=thongtinraovat&matin=<?php echo $n['matin']; ?>"><img src="images/chitiet.png" border="0" title="Chi tiết tin rao vặt" /></a></td><td style="text-align:center"><a href="home_admin.php?module=xoa&field=matin&table=tinraovat&value=<?php echo $n['matin']; ?>"><img src="images/Sign-Error-icon1.png" border="0" title="Xóa tin rao vật" /></a></td></tr>
		<?php
			$i++;
			}
		?>
		</table>
	<?php
		
		if($sotrang>1)
			{
		 	if($ar[0]!=1 && isset($_GET['page']))
				$color='#0033CC';
			else
				$color='#000000';
			$start="1:0,".$dong_trang;
			?>
			<div> Pages &nbsp;
			<?php
				echo "<a href='home_admin.php?module=timkiem&doituong=$doituong&tukhoa=$tukhoa&page=$start' style='color:$color'>1</a>";
			for($i=2;$i<=$sotrang;$i++)
			{
				if($ar[0]==$i)
					$color='#000000';
				else
					$color='#0033CC';
				$p=($i-1)*$dong_trang;
				$page=$i.":".$p.",".$dong_trang;
				echo "&nbsp;<a href='home_admin.php?module=timkiem&doituong=$doituong&tukhoa=$tukhoa&page=$page&i=$p' style='color:$color'>$i</a>";	
			}
		}
		}
		if($c<1)
			echo "<br>";
	?>
	<br /><br><label style="text-align:center" class="button"><a class="menulink" href="home_admin.php?module=tinraovat"><img src="images/add.png" border="0">&nbsp;Thêm tin mới</a></label>
	<?php
	}
	else if($doituong=="tintuc")
	{
		$kq=$knoi->queryCsdl("select matt,tieude,ngaydang,trangthai from tintuc a,loaitintuc b where a.maloaitt=b.maloaitt and (tieude like '%$tukhoa%' or a.noidung like '%$tukhoa%' or tenloai like '%$tukhoa%')");
		if(mysql_num_rows($kq)<1)
					{
						echo "<br><label class='stylelitle'>Không tìm thấy tin tức nào có từ khóa \"$tukhoa\"!</label>";
					}
					else
					{
					$tongdong=mysql_num_rows($kq);
					$dong_trang=2;
					$sotrang=ceil($tongdong/$dong_trang);
					
					$limit="0,".$dong_trang;
					if(isset($_GET['page']))
					{
						$t=$_GET['page'];
						$ar=explode(":",$t);
						$limit=$ar[1];
					}
					//echo $limit;
					if($sotrang>1)
						$kq=$knoi->queryCsdl("select matt,tieude,ngaydang,trangthai from tintuc a,loaitintuc b where a.maloaitt=b.maloaitt and (tieude like '%$tukhoa%' or a.noidung like '%$tukhoa%' or tenloai like '%$tukhoa%') limit $limit");
				?>
			  <table align="center" style="border:solid #FFFFFF; background-color:#F5F5F5">
						<tr class="stylelitle"  style="text-align:center; background-color:#CCFFFF"><td style="text-align:right">Stt&nbsp;</td><td style="text-align:center;">Tiêu đề</td><td>Ngày đăng</td><td>Trạng thái</td><td>Chi tiết</td><td>Xóa</td></tr>
					<?php
						$i=1;
						if(isset($_GET['i']))
							$i=$_GET['i'] + 1;
						while($n=mysql_fetch_array($kq))
						{
							if($i%2==0)
								$color='#FFFFCC';
							else
								$color='#FFFFFF';
					?>
						<tr  class="classtr" style="text-align:left; background-color:<?php echo $color; ?>;"><td><?php echo $i; ?></td><td><?php echo $n['tieude']; ?></td>
						<td>
						<?php 
							$date=$n['ngaydang'];
							//echo $date;
							$date=$knoi->formatDate($date); 
							echo $date; 
						?>						</td>
						<td><?php if($n['trangthai']==0) echo "Lưu"; else echo "Đăng"; ?></td><td style="text-align:center"><a  href="home_admin.php?module=chitiettintuc&matt=<?php echo $n['matt']; ?>"><img src="images/chitiet.png" border="0" title="Chi tiết loại tin tức" /></a></td>
						<td style="text-align:center;"><a href="home_admin.php?module=xoa&field=matt&table=tintuc&value=<?php echo $n['matt']; ?>"><img src="images/Sign-Error-icon1.png" border="0" title="Xóa tin tức" /></a></td></tr>
					<?php
						$i++;
						}
					?>
			  </table>
				<?php
					}
			if($sotrang>1)
			{
		 	if($ar[0]!=1 && isset($_GET['page']))
				$color='#0033CC';
			else
				$color='#000000';
			$start="1:0,".$dong_trang;
			?>
			<div> Pages &nbsp;
			<?php
				echo "<a href='home_admin.php?module=timkiem&doituong=$doituong&tukhoa=$tukhoa&page=$start' style='color:$color'>1</a>";
			for($i=2;$i<=$sotrang;$i++)
			{
				if($ar[0]==$i)
					$color='#000000';
				else
					$color='#0033CC';
				$p=($i-1)*$dong_trang;
				$page=$i.":".$p.",".$dong_trang;
				echo "&nbsp;<a href='home_admin.php?module=timkiem&doituong=$doituong&tukhoa=$tukhoa&page=$page&i=$p' style='color:$color'>$i</a>";	
			}
			}
			?>
			<br><br /><label style="text-align:center" class="button"><a class="menulink" href="home_admin.php?module=tintuc&loai=<?php echo $loai; ?>&loaitin=<?php echo $madt; ?>"><img src="images/add.png" border="0">&nbsp;Tin tức mới</a></label>
			<?php
	}
	else
	{
		$kq=$knoi->queryCsdl("select * from loaitintuc where tenloai like '%$tukhoa%' or noidung like '%$tukhoa%'");
		if(mysql_num_rows($kq)<1)
		{
			echo "<br><label class='stylelitle'>Không tìm thấy loại tin nào với từ khóa \"".$tukhoa."\"!</label>";
		}
		else
		{
	?>
				<table align="center" style="border:solid #FFFFFF; background-color:#F5F5F5">
						<tr class="stylelitle"  style="text-align:center; background-color:#CCFFFF"><td style="text-align:right">Stt&nbsp;</td><td style="text-align:center;">Mã loại</td><td>Tên loại</td><td>Chi tiết</td><td>Xóa</td></tr>
					<?php
						$i=1;
						while($n=mysql_fetch_array($kq))
						{
							if($i%2==0)
						$color='#FFFFCC';
					else
						$color='#FFFFFF';
					?>
						<tr  class="classtr" style="text-align:left; background-color:<?php echo $color; ?>;"><td style="text-align:right"><?php echo $i; ?>&nbsp;</td><td style="text-align:center"><?php echo $n['maloaitt']; ?></td><td>&nbsp;<?php echo $n['tenloai']; ?></td><td style="text-align:center"><a  href="home_admin.php?module=loaitintuc&maloaitt=<?php echo $n['maloaitt']; ?>"><img src="images/chitiet.png" border="0" title="Chi tiết loại tin tức" /></a></td><td style="text-align:center"><a href="home_admin.php?module=xoa&field=maloaitt&table=loaitintuc&value=<?php echo $n['maloaitt']; ?>"><img src="images/Sign-Error-icon1.png" border="0" title="Xóa loại tin tức" /></a></td></tr>
					<?php
						$i++;
						}
					?>
				</table>
				<?php } ?>
				<br><br /><label style="text-align:center" class="button"><a href="home_admin.php?module=loaitintuc"><img src="images/add.png" border="0">&nbsp;Loại tin tức mới</a></label>
				<?php
		
	}
?>